// 完整的treeData
const treeData = [  {
    nodePath: 'request',
    code: 'request',
    name: 'request',
    type: 'object',
    required: '1',
    children: [
      {
        nodePath: 'request.mock',
        code: 'mock',
        name: 'mock标识',
        required: '0',
        type: 'string',
        children: [],
      },
      {
        nodePath: 'request.requestData',
        code: 'requestData',
        name: '业务数据',
        required: '0',
        type: 'Object',
        children: [ 
          {
            nodePath: 'request.requestData.loginId',
            code: 'loginId',
            name: '员工号',
            required: '0',
            type: 'string',
            children: [],
          },
          {
            nodePath: 'request.requestData.newPwd',
            code: 'newPwd',
            name: '新密码',
            required: '0',
            type: 'string',
            children: [],
          },
          {
            nodePath: 'request.requestData.verifyCode',
            code: 'verifyCode',
            name: '凭证',
            required: '0',
            type: 'string',
            children: [],
          }
        ],
      },
      {
        nodePath: 'request.requestId',
        code: 'requestId',
        name: '请求流水号',
        required: '0',
        type: 'string',
      },
      {
        nodePath: 'request.timestamp',
        code: 'timestamp',
        name: '时间戳',
        required: '0',
        type: 'integer',
        children: [],
      }
    ],
  }
];
// 接口tableData
const tableData = [
  {
    code: "request",
    name: "request",
    required: "1",
    type: "object",
    checkRule: "2",
    fillRule: "1",
    fillValue: "111111111",
    children: [
      {
        code: "mock",
        name: "Mock标识",
        required: "1",
        maxLen: 11,
        type: "boolean",
        checkRule: "1",
        fillRule: "2",
        fillValue: "AAAAAAAAAAAA",
        children: [],
      }
    ],
  }
]
// // 根据名称在树形数据源中查找节点
// function findNodeByName(tree, code) {
//   for (const node of tree) {
//     if (node.code === code) {
//       return node;
//     }
//     if (node.children) {
//       const found = findNodeByName(node.children, code);
//       if (found) {
//         return found;
//       }
//     }
//   }
//   return null;
// }

// 更新表格数据，添加 nodePath
// function updateTableDataWithNodePath(tableData, treeData) {
//   if(!Array.isArray(tableData) || tableData.length === 0) {
//     return [];
//   }
//   const updatedTableData = tableData.map(item => {
//     const matchingNode = findNodeByName(treeData, item.code);
//     const updatedItem = {
//         ...item,
//         nodePath: matchingNode ? matchingNode.nodePath : `temp-${Math.random().toString(36).substr(2)}`,
//         required: Number(item.required),
//         children: item.children ? updateTableDataWithNodePath(item.children, treeData) : []
//       };
//     // if(item.children && item.children.length > 0) {
//     //   updatedItem.children = updateTableDataWithNodePath(item.children, treeData);
//     // }

//     return updatedItem;
//   });
//   return updatedTableData;
// }

// 执行函数
// const updatedTableData = updateTableDataWithNodePath(tableData, treeData);
// console.log('最终的表格数据:', updatedTableData);


// 递归函数为每个节点添加 nodePath
function addNodePath(data, parentPath = '') {
  return data.map(item => {
    const currentNodePath = parentPath ? `${parentPath}.${item.code}` : item.code;
    const updatedItem = {
      ...item,
      nodePath: currentNodePath,
      children: item.children ? addNodePath(item.children, currentNodePath) : [],
    };
    return updatedItem;
  });
}

// 执行函数
const updatedTableData = addNodePath(tableData);
console.log('最终的表格数据:', JSON.stringify(updatedTableData, null, 2));